package com.ems.service.impl.system;

import com.ems.pojo.user.User;
import com.ems.service.system.SystemService;
import com.ems.service.user.UserService;
import com.ems.utils.SendEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Auther: 伟伟
 * @Date: 2020/3/19
 * @Description: 描述
 * @Version: 1.0
 */
@Service
public class SystemServiceImpl implements SystemService {
    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @Override
    public int findPassword(User user) {
        //判断用户输入的qq邮箱查询用户
        User user1 = userService.getUserByEmail(user.getUser_email());
        if(user1 == null && user1.getId() == 0){
            return 0;
        }else{
            //一致的话，向指定邮箱发送密码

            //重置用户密码
            String newPawd = UUID.randomUUID().toString().replace("-","").substring(0,10);
            user.setUser_password(newPawd);
            userService.updateUserPasswordById(user1);

            SendEmail sendEmail = new SendEmail(user1.getUser_email(),"考试平台密码找回","密码已重置为"+newPawd+",请尽快登录修改密码");
            boolean isSend = sendEmail.send();
            if(isSend){
                return 1;
            }else{
                return -1;
            }

        }

    }
}
